// pages/myorder/myorder.js
var app=new getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allGoodsorder: [],
    address_id:"",
    address_name:"",
    address_phone:"",
    address_region:"",
    address_address:"",
    address_hide:true
  },
  addressTo:function(){
    wx.navigateTo({
      url: '/pages/selectaddress/selectaddress'
    })
  },
  submit:function(){
    var that = this;
    var back_ids = [];
    var back_idstring = app.backpack_record.backpack_order;
    for (var i = 0; i < back_idstring.length; i++) {
      back_ids.push(parseInt(back_idstring[i]))
    }
    console.log(app.user.token, back_ids);
    wx.request({
      url: app.globalData.api_url + "/OpenAPI/V1/User/orderPost",
      data: {
        token: app.user.token, 
        ress_id: app.choice_address.id, 
        toy_ids: back_ids,
        iscx: 1
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        wx.navigateBack({
          delta:1
        });
      },
      fail: function (res) {
        console.log(res.data)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    wx.checkSession({
          success: function () {
            //session 未过期，并且在本生命周期一直有效
          },
          fail: function () {
            //登录态过期
            wx.login() //重新登录
      }
    })
    var that = this;
    wx.request({
      url: app.globalData.api_url + "/OpenAPI/V1/Backpack/getAll",
      data: {
        token: app.user.token,
        page: 1
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        var list = res.data.data.list;
        var backpack_order = app.backpack_record.backpack_order;
        var backpackArr=[];
        for(var i=0;i<list.length;i++){
          list[i]['num']=1;
          if (backpack_order.indexOf(list[i]['backpack_id'])>=0){
            backpackArr.push(list[i]);
            that.setData({
              allGoodsorder: backpackArr
            })
          }
        }
        console.log(backpackArr)
      },
      fail: function (res) {
        console.log(res.data)
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
     
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(app.choice_address);
    this.setData({
      address_id: app.choice_address.id,
      address_name: app.choice_address.name,
      address_phone: app.choice_address.phone,
      address_region: app.choice_address.region,
      address_address: app.choice_address.address,
      address_hide:false
    })
    if (app.choice_address.name==""){
      this.setData({
        address_hide: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})